﻿namespace DFGJ.WMS_PDA.WebAPI.Extensions
{
    /// <summary>
    /// Cors扩展
    /// </summary>
    public static class CorsExtend
    {
        /// <summary>
        ///  注入Cors
        /// </summary>
        /// <param name="builder"></param>
        public static void AddCorsSetUp(this WebApplicationBuilder builder)
        {
            string MyAllowSpecificOrigins = builder.Configuration["MyAllowSpecificOrigins"];
            if (string.IsNullOrEmpty(MyAllowSpecificOrigins))
            {
                throw new Exception("Cors参数为空");
            }
            builder.Services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  policy =>
                                  {
                                      policy.AllowAnyOrigin()
                                            .AllowAnyHeader()
                                            .AllowAnyMethod();

                                  });
            });

        }

        /// <summary>
        /// Cors中间件
        /// </summary>
        /// <param name="builder"></param>
        /// <exception cref="Exception"></exception>
        public static void UseCorsExtend(this WebApplication builder)
        {
            string MyAllowSpecificOrigins = builder.Configuration["MyAllowSpecificOrigins"];
            if (string.IsNullOrEmpty(MyAllowSpecificOrigins))
            {
                throw new Exception("Cors参数为空");
            }
            builder.UseCors(MyAllowSpecificOrigins);

        }



    }
}
